//发现模块tabbar
import 'package:flutter/material.dart';
import 'package:xvideos/assets_tool/images.dart';

class CommunitySectionTabMenu extends StatefulWidget {
  final int curIndex;
  final List<String> tabTitles;
  final TabController? tabController;
  final double? fontSize;

  const CommunitySectionTabMenu(
    this.tabTitles, {
    super.key,
    this.curIndex = 0,
    this.tabController,
    this.fontSize = 16,
  });

  @override
  State<StatefulWidget> createState() {
    return _CommunitySectionTabMenuState();
  }
}

class _CommunitySectionTabMenuState extends State<CommunitySectionTabMenu> {
  late int curIndex = widget.curIndex;
  final itemKeys = [];

  @override
  void initState() {
    super.initState();
    widget.tabController?.addListener(_listener);
  }

  void _listener() {
    print('_listener ${widget.tabController?.offset}');
    int tabIndex = widget.tabController?.index ?? 0;
    if (tabIndex != curIndex) {
      curIndex = tabIndex;
    }
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      height: 36,
      child: TabBar(
        tabAlignment: TabAlignment.start,
        controller: widget.tabController,
        indicatorWeight: 0,
        isScrollable: true,
        labelPadding: EdgeInsets.symmetric(horizontal: 8),
        labelStyle: TextStyle(fontSize: widget.fontSize, fontWeight: FontWeight.w600),
        unselectedLabelStyle: TextStyle(fontSize: widget.fontSize, fontWeight: FontWeight.w400),
        indicator: BoxDecoration(),
        tabs: List.generate(
          widget.tabTitles.length,
          (index) => getTabItem(curIndex == index, widget.tabTitles[index]),
        ),
      ),
    );
  }

  Widget getTabItem(bool isSelect, String title) {
    if (isSelect) {
      if (title == '社区') {
        return Image.asset('community_main_sel.png'.communityPath, width: 68);
      } else if (title == '图集') {
        return Image.asset('community_pic_sel.png'.communityPath, width: 68);
      } else if (title == '黄游') {
        return Image.asset('community_game_sel.png'.communityPath, width: 68);
      } else if (title == '小说') {
        return Image.asset('community_noval_sel.png'.communityPath, width: 68);
      } else {
        return Container();
      }
    } else {
      return Container(
        width: 49,
        alignment: Alignment.center,
        child: Text(
          title,
        ),
      );
    }
  }

  @override
  void dispose() {
    widget.tabController?.removeListener(_listener);
    super.dispose();
  }
}
